﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using VietPremier.Models;
using VietPremier.Repository;
using PagedList;

namespace VietPremier.Controllers
{
    public class TravelInformationController : Controller
    {
        private int pageSize = 20;

        public ViewResult Information()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                GetTravelInformationModel model = new GetTravelInformationModel();

                EntityTravelInfo travelInfo = db.EntityTravelInfos.FirstOrDefault();
                if (travelInfo != null)
                {
                    model.travelInfo.Description = travelInfo.Description;
                    //SEO
                    model.travelInfo.Titletag = travelInfo.Titletag;
                    model.travelInfo.Meta_description = travelInfo.Meta_description;
                    model.travelInfo.Meta_keyword = travelInfo.Meta_keyword;

                    foreach (var travelProduct in db.EntityTravelProductions.Where(o => o.IdTravelInfo == travelInfo.Id).OrderBy(o => o.Position))
                    {
                        model.listTravelProduct.Add(new TravelProduct()
                        {
                            Id = travelProduct.Id,
                            Name = travelProduct.Name,
                            Image = travelProduct.Image,
                            Description = travelProduct.Description
                        });

                    }
                    return View(model);
                }
                return View();
            }
        }

        public ViewResult Product(string title)// title của product
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int id = Utilities.GetIdByTitle(title, VietPremierMenu.TravelProduct, VietPremierDetailList.Detail);

                GetTravelProductModel model = new GetTravelProductModel();
                EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == id);
                if (travelProduct != null)
                {
                    model.travelProduct.Name = travelProduct.Name;
                    model.travelProduct.About = travelProduct.About;
                    //SEO
                    model.travelProduct.Titletag = travelProduct.Titletag;
                    model.travelProduct.Meta_description = travelProduct.Meta_description;
                    model.travelProduct.Meta_keyword = travelProduct.Meta_keyword;

                    foreach (var travelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == travelProduct.Id).OrderBy(o => o.Position))
                    {
                        if (travelCity != null)
                        {
                            model.listTravelCity.Add(new TravelCity()
                            {
                                Id = travelCity.Id,
                                Name = travelCity.Name,
                                Image = travelCity.Image,
                                Description = travelCity.Description
                            });

                            //foreach (var travelHotel in db.EntityTravelHotels.Where(o => o.IdTravelCity == travelCity.Id).Take(10))
                            //{
                            //    model.listTravelHotel.Add(new TravelHotel()
                            //    {
                            //        //lấy tên của travelCity
                            //        travelCity = new TravelCity() { Name = travelCity.Name },
                            //        Name = travelHotel.Name,
                            //        Image = travelHotel.Image
                            //    });
                            //}
                        }

                    }

                    foreach (var travelHotel in db.EntityTravelHotels.Take(10))
                    {
                        model.listTravelHotel.Add(new TravelHotel()
                        {
                            Name = travelHotel.Name,
                            Image = travelHotel.Image
                        });
                    }

                    return View(model);

                }
                return View();
            }
        }

        public ViewResult City(string title)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int id = Utilities.GetIdByTitle(title, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);

                GetTravelCityModel model = new GetTravelCityModel();
                EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == id);
                
                if (travelCity != null)
                {
                    EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == travelCity.IdTravelProduction);
                    if (travelProduct != null)
                    {
                        model.travelProduct.Name = travelProduct.Name;
                    }
                    model.travelCity.Name = travelCity.Name;
                    model.travelCity.Body = travelCity.Body;
                    model.travelCity.Itinerary = travelCity.Itinerary;
                    model.travelCity.Note = travelCity.Note;
                    model.travelCity.Image = travelCity.Image;
                    model.travelCity.Description = travelCity.Description;
                    model.travelCity.Price = travelCity.Price;
                    model.travelCity.Time = travelCity.Time;
                    model.travelCity.Duration = travelCity.Duration;

                    //SEO
                    model.travelCity.Titletag = travelCity.Titletag;
                    model.travelCity.Meta_description = travelCity.Meta_description;
                    model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                    foreach (var listTravelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == travelCity.IdTravelProduction && o.Id != travelCity.Id).Take(4))
                    {
                        model.listTravelCity.Add(new TravelCity()
                        {
                            Id = listTravelCity.Id,
                            Name = listTravelCity.Name,
                            Image = listTravelCity.Image,
                            Price = listTravelCity.Price,
                            Time = listTravelCity.Time,
                            Duration = listTravelCity.Duration,
                            Description = listTravelCity.Description
                        });
                    }

                    return View(model);
                }
                return View();
            }
        }

        [HttpPost]
        public ActionResult City(string title, GetTravelCityModel model)
        {
            if (ModelState.IsValid)
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    int id = Utilities.GetIdByTitle(title, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);
                    EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == id);

                    StringBuilder mailBody = new StringBuilder();
                    mailBody.AppendFormat("<h1>Program Information</h1>");
                    mailBody.AppendFormat("<table><tr><td style='width:200px'>Tour:</td><td>" + model.travelCity.Name + "</td></tr>");
                    mailBody.AppendFormat("<tr style='height:20px'></tr> ");
                    mailBody.AppendFormat("<tr><td>Họ tên:</td><td>" + model.Gender + " " + model.FullName + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Đơn vị/ Doanh nghiệp:</td><td>" + model.Supplier + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Nơi đến:</td><td>" + model.Destination + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Quốc tịch:</td><td>" + model.Nationality + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Điện thoại:</td><td>" + model.Telephone + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Email:</td><td>" + model.Email + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng người lớn:</td><td>" + model.NumberofAdults + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng trẻ em:</td><td>" + model.NumberofChildren + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày đi:</td><td>" + model.DateFrom + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày về:</td><td>" + model.DateTo + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Chuẩn khách sạn:</td><td>" + model.Place + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Yêu cầu đặc biệt khác:</td><td>" + model.SpecialRequest + "</td></tr></table>");
                    mailBody.AppendFormat("<br></br>");
                    mailBody.AppendFormat("<p>Email được gửi từ một email ảo, No reply</p>");

                    try
                    {
                        MailHelper.SendMailMessage("noreply@khainguyenepco.vn", "sales@vietnampremier.com", "hmduc@khainguyenepco.vn", "khoa@vietnampremier.com", "subject", mailBody.ToString(), "smtp.gmail.com", true, "noreply@khainguyenepco.vn", "kh@inguyen");
                        EntityFeedBack feedback = new EntityFeedBack();
                        feedback.Email = model.Email;
                        feedback.DateCreate = DateTime.Now;
                        feedback.Body = mailBody.ToString();
                        feedback.Page = "Promotion";
                        db.EntityFeedBacks.InsertOnSubmit(feedback);
                        db.SubmitChanges();

                         model = new GetTravelCityModel();                    
                        if (travelCity != null)
                        {
                            EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == travelCity.IdTravelProduction);
                            if (travelProduct != null)
                            {
                                model.travelProduct.Name = travelProduct.Name;
                            }
                            model.travelCity.Name = travelCity.Name;
                            model.travelCity.Body = travelCity.Body;
                            model.travelCity.Itinerary = travelCity.Itinerary;
                            model.travelCity.Note = travelCity.Note;
                            model.travelCity.Image = travelCity.Image;
                            model.travelCity.Description = travelCity.Description;
                            model.travelCity.Price = travelCity.Price;
                            //model.travelCity.TripCode = travelCity.TripCode;
                            model.travelCity.Time = travelCity.Time;
                            model.travelCity.Duration = travelCity.Duration;

                            //SEO
                            model.travelCity.Titletag = travelCity.Titletag;
                            model.travelCity.Meta_description = travelCity.Meta_description;
                            model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                            foreach (var listTravelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == travelCity.IdTravelProduction && o.Id != travelCity.Id).Take(4))
                            {
                                model.listTravelCity.Add(new TravelCity()
                                {
                                    Id = listTravelCity.Id,
                                    Name = listTravelCity.Name,
                                    Image = listTravelCity.Image,
                                    Price = listTravelCity.Price,
                                    //TripCode = listTravelCity.TripCode,
                                    Time = listTravelCity.Time,
                                    Duration = listTravelCity.Duration,
                                    Description = listTravelCity.Description
                                });
                            }

                            TempData["Message"] = "Success";
                        }
                    }
                    catch
                    {
                        model = new GetTravelCityModel();
                        if (travelCity != null)
                        {
                            EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == travelCity.IdTravelProduction);
                            if (travelProduct != null)
                            {
                                model.travelProduct.Name = travelProduct.Name;
                            }
                            model.travelCity.Name = travelCity.Name;
                            model.travelCity.Body = travelCity.Body;
                            model.travelCity.Itinerary = travelCity.Itinerary;
                            model.travelCity.Note = travelCity.Note;
                            model.travelCity.Image = travelCity.Image;
                            model.travelCity.Description = travelCity.Description;
                            model.travelCity.Price = travelCity.Price;
                            //model.travelCity.TripCode = travelCity.TripCode;
                            model.travelCity.Time = travelCity.Time;
                            model.travelCity.Duration = travelCity.Duration;

                            //SEO
                            model.travelCity.Titletag = travelCity.Titletag;
                            model.travelCity.Meta_description = travelCity.Meta_description;
                            model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                            foreach (var listTravelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == travelCity.IdTravelProduction && o.Id != travelCity.Id).Take(4))
                            {
                                model.listTravelCity.Add(new TravelCity()
                                {
                                    Id = listTravelCity.Id,
                                    Name = listTravelCity.Name,
                                    Image = listTravelCity.Image,
                                    Price = listTravelCity.Price,
                                    //TripCode = listTravelCity.TripCode,
                                    Time = listTravelCity.Time,
                                    Duration = listTravelCity.Duration,
                                    Description = listTravelCity.Description
                                });
                            }

                            TempData["Message"] = "ErrorMessage";
                        }
                    }
                    return View(model);
                }
            }
            else
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    int id = Utilities.GetIdByTitle(title, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);
                    EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == id);

                    if (travelCity != null)
                    {
                        EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == travelCity.IdTravelProduction);
                        if (travelProduct != null)
                        {
                            model.travelProduct.Name = travelProduct.Name;
                        }
                        model.travelCity.Name = travelCity.Name;
                        model.travelCity.Body = travelCity.Body;
                        model.travelCity.Itinerary = travelCity.Itinerary;
                        model.travelCity.Note = travelCity.Note;
                        model.travelCity.Image = travelCity.Image;
                        model.travelCity.Description = travelCity.Description;
                        model.travelCity.Price = travelCity.Price;
                        //model.travelCity.TripCode = travelCity.TripCode;
                        model.travelCity.Time = travelCity.Time;
                        model.travelCity.Duration = travelCity.Duration;

                        //SEO
                        model.travelCity.Titletag = travelCity.Titletag;
                        model.travelCity.Meta_description = travelCity.Meta_description;
                        model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                        foreach (var listTravelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == travelCity.IdTravelProduction && o.Id != travelCity.Id).Take(4))
                        {
                            model.listTravelCity.Add(new TravelCity()
                            {
                                Id = listTravelCity.Id,
                                Name = listTravelCity.Name,
                                Image = listTravelCity.Image,
                                Price = listTravelCity.Price,
                                //TripCode = listTravelCity.TripCode,
                                Time = listTravelCity.Time,
                                Duration = listTravelCity.Duration,
                                Description = listTravelCity.Description
                            });
                        }
                        TempData["ContactUs"] = "Error";
                        return View(model);
                    }
                    return View(model);
                }

            }
        }

        public ViewResult Hotel(int page = 1)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                GetTravelHotelModel model = new GetTravelHotelModel();
              
                foreach (var travelHotel in db.EntityTravelHotels.Skip((page - 1) * pageSize).Take(pageSize))
                {
                    model.listTravelHotel.Add(new TravelHotel()
                    {
                        Id = travelHotel.Id,
                        Name = travelHotel.Name,
                        Image = travelHotel.Image,
                        Rate = travelHotel.Rate,
                        Address = travelHotel.Address,
                        Phone = travelHotel.Phone,
                        Comment = travelHotel.Comment
                        

                    });
                }
                model.PagingInfo = new PagingInfo()
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = db.EntityTravelHotels.Count()
                };
                return View(model);
              
             }           
        }

        #region "Search"
        public ActionResult Search(int? idProduct, int? idStyle, string Time, int? page)
        {
            //int idCountry, int idStyle, int idDestination,
            if (Time == null)
                Time = "";
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {

                var query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            join t in db.EntityTravelStyles on q.Id equals t.IdTravelCity
                            where p.Id == idProduct && t.IdStyle == idStyle
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,                                
                            };
                if (idProduct != null && idStyle != null && Time.Length == 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            join t in db.EntityTravelStyles on q.Id equals t.IdTravelCity
                            where p.Id == idProduct && t.IdStyle == idStyle
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }

                else if (idProduct != null && idStyle == null && Time.Length == 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            where p.Id == idProduct
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }
                else if (idProduct != null && idStyle == null && Time.Length != 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            where p.Id == idProduct && q.Time == Time
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }
                else if (idProduct == null && idStyle != null && Time.Length != 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            join t in db.EntityTravelStyles on q.Id equals t.IdTravelCity
                            where t.IdStyle == idStyle && q.Time == Time
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }
                else if (idProduct == null && idStyle != null && Time.Length == 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            join t in db.EntityTravelStyles on q.Id equals t.IdTravelCity
                            where t.IdStyle == idStyle
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }
                else if (idProduct == null && idStyle == null && Time.Length != 0)
                {
                    query = from p in db.EntityTravelProductions
                            join q in db.EntityTravelCities on p.Id equals q.IdTravelProduction
                            where q.Time == Time
                            select new TravelCity
                            {
                                Id = q.Id,
                                IdTravelProduct = q.IdTravelProduction,
                                ProductName = p.Name,
                                Image = q.Image,
                                Price = q.Price,
                                Name = q.Name,
                                Time = q.Time,
                                Duration = q.Duration,
                            };
                }

                if (idProduct != null)
                {
                    ViewBag.idProduct = idProduct;
                }
                if (idStyle != null)
                {
                    ViewBag.idStyle = idStyle;
                }
                if (Time.Length != 0)
                {
                    ViewBag.Time = Time;
                }

                if (query.Count() != 0)
                {

                    List<TravelCity> lstTravel = new List<TravelCity>();
                    lstTravel = query.ToList();

                    if (Request.HttpMethod != "GET")
                    {
                        page = 1;
                    }

                    int pageSize = 10;
                    int pageNumber = (page ?? 1);
                    return View(lstTravel.ToPagedList(pageNumber, pageSize));
                }
                else
                    return View("NoData");

            }
        }
        #endregion

        #region "No Data"
        public ActionResult NoData()
        {
            return View();
        }
        #endregion
    }
}
